package yxy.game.pm2.active.command.weeklycard;

import org.joda.time.DateTime;
import yxy.apple.logger.Logger;
import yxy.cherry.data.bean.actweekcarkgift;
import yxy.cherry.data.string.GameString;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_周卡;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.active.module.subordinateclass.weeklycard.WeeklyWish;
import yxy.game.pm2.active.module.timemail.TimeMail;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.module.mail.MailTypeEnum;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 周卡_许愿 选择
 * @author Dell
 *
 */
@MessageType(proto = Command.ActWeeklyCardWishSelect, classtype = ActiveMessage.ActWeeklyCardWishSelectRequest.class)
public class CMD_Active_周卡_许愿_Select extends Command<ActiveMessage.ActWeeklyCardWishSelectRequest> {

	private static final Logger logger = Logger.create(CMD_Active_周卡_许愿_Select.class);
	
	@Override
	public void execute(MessageSource messageSource, ActiveMessage.ActWeeklyCardWishSelectRequest request) throws Exception {
		String playercode = messageSource.getTag();
		List<Integer> selectids = request.getSelectidsList();
		ActiveMessage.ActWeeklyCardWishSelectResponse.Builder response = ActiveMessage.ActWeeklyCardWishSelectResponse.newBuilder();
		
		ActiveModule_周卡 am = ActiveModule_周卡.get(playercode,false);
		if(am == null) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}
		
		if(!am.running()) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}

		if(am.getWw().getSelectids().size() > 0) {
			send(messageSource, response, Result.ActHadSelect);
			return;
		}
		
		WeeklyWish ww = am.getWw();
		
		for(int selectid : selectids) {
			if(!ww.getIds().contains(selectid)) {
				send(messageSource, response, Result.DataError);
				return;
			}
			
			if(!ww.getUnlockids().contains(selectid)) {
				send(messageSource, response, Result.ActNOUnlock);
				return;
			}
		}
		
		ww.getSelectids().addAll(selectids);
		am.set();
		
		//创建延时邮件
		Map<String, actweekcarkgift> awcgmap = ActDataCenter.getAllData(actweekcarkgift.class, playercode);
		ArrayList<Currency> curs = new ArrayList<Currency>();
		for(int selectid : selectids) {
			actweekcarkgift data = awcgmap.get(String.valueOf(selectid));
			Currency cur = Currency.create(data.getRewardtype(), data.getRewardid(), data.getRewardnum());
			curs.add(cur);
		}
		
		try {
			if(TimeMail.lockTimeMail()) {
				DateTime needsendtime = DateTime.now().plusDays(1);
				String title = GameString.周卡许愿奖励_titl().getText();
				String content = GameString.周卡许愿奖励_msg().getText();
				
				TimeMail.create(needsendtime, playercode, MailTypeEnum.Mail, title, content, CCReason.活动_周卡, curs);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			TimeMail.unlockTimeMail();
		}
		
		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, ActiveMessage.ActWeeklyCardWishSelectResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}
